package com.wzh.live.service.impl;

import com.wzh.live.dao.LiveGoodsDao;
import com.wzh.live.entity.LiveGoodsEntity;
import com.wzh.live.service.LiveGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;

import com.wzh.live.dao.LiveStreamDao;
import com.wzh.live.entity.LiveStreamEntity;
import com.wzh.live.service.LiveStreamService;


@Service("liveStreamService")
public class LiveStreamServiceImpl extends ServiceImpl<LiveStreamDao, LiveStreamEntity> implements LiveStreamService {

    @Autowired
    private LiveGoodsService liveGoodsService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<LiveStreamEntity> page = this.page(
                new Query<LiveStreamEntity>().getPage(params),
                new QueryWrapper<LiveStreamEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void addLive(LiveStreamEntity liveStream) {
        int insert = baseMapper.insert(liveStream);
        if (insert>0){
            List<LiveGoodsEntity> collect = liveStream.getSelectionGood().stream().map(item -> {
                item.setLiveId(liveStream.getLiveId());
                return item;
            }).collect(Collectors.toList());
            liveGoodsService.saveBatch(collect);
        }
    }

    @Override
    public void updateLive(LiveStreamEntity liveStream) {
        int insert = baseMapper.updateById(liveStream);
        if (insert>0){
            List<LiveGoodsEntity> collect = liveStream.getSelectionGood().stream().map(item -> {
                item.setLiveId(liveStream.getLiveId());
                return item;
            }).collect(Collectors.toList());
            liveGoodsService.saveBatch(collect);
        }
    }
}